﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.IO;

namespace Servidor
{
    public class Conexion
    {
        public MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;
        private string connectionString;

        public Conexion(int tipo, string pNombreBaseDatos)
        {
            if (tipo == 1)
            {
                Iniciar_db();
            }
            else
            {
                Iniciar_dbcuentas(pNombreBaseDatos);
            }

            connection = new MySqlConnection(connectionString);
        }

        public void Iniciar_db()
        {
            server = "localhost";
            database = "sia_db";
            uid = "root";
            password = "admin";
            connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
		    database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";    
        }

        public void Iniciar_dbcuentas(string pNombreBaseDatos)
        {
            server = "localhost";
            database = pNombreBaseDatos;
            uid = "root";
            password = "admin";
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
        }

        public void crearNuevaBaseDatos(String pNombre)
        {
            MySqlConnection tempConn = new MySqlConnection("server=localhost;user=root;password=admin");
            tempConn.Open();
            MySqlCommand command = new MySqlCommand("create database if not exists " + pNombre + ";", tempConn);
            MySqlCommand command2 = new MySqlCommand("use " + pNombre + ";", tempConn);
            command.ExecuteNonQuery();
            command2.ExecuteNonQuery();

            MySqlScript script1 = new MySqlScript(tempConn, File.ReadAllText("C:\\Users\\Andres\\Desktop\\sia-erp2\\Servidor\\ScriptBDxEmpresa.sql"));

            script1.Delimiter = ";";
            script1.Execute();


            MySqlScript script = new MySqlScript(tempConn, File.ReadAllText("C:\\Users\\Andres\\Desktop\\sia-erp2\\Servidor\\ScriptProcedimientosBDxEmpresa.sql"));

            script.Delimiter = "//";
            script.Execute();

            tempConn.Close();
        }

        public bool AbrirConexion()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                return false;
            }
        }

        public bool CerrarConexion()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                return false;
            }
        }
    }
}